package com.neuedu.dao;

import com.neuedu.entity.Admin;
import com.neuedu.util.JdbcUtil;

import java.util.List;

public class AdminDaoImpl implements AdminDao{
    @Override
    public List<Admin> list() {
        return JdbcUtil.executeQuery("""
                select
                    id,
                    name,
                    phone,
                    sex,
                    email,
                    birthday,
                    password,
                    create_date as createDate,
                    create_time as createTime
                from
                    admin
                """, Admin.class);
    }

    @Override
    public int add(Admin admin) {
        return JdbcUtil.executeUpdate("insert into admin(name, phone, sex, email, birthday, password, create_date, create_time) values(?, ?, ?, ?, ?, ?, ?, ?)",
                admin.getName(), admin.getPhone(), admin.getSex(), admin.getEmail(), admin.getBirthday(), admin.getPassword(), admin.getCreateDate(), admin.getCreateTime());
    }

    @Override
    public Admin getById(Integer id) {
        return JdbcUtil.executeQueryOne("""
                select
                    id,
                    name,
                    phone,
                    sex,
                    email,
                    birthday,
                    password,
                    create_date as createDate,
                    create_time as createTime
                from
                    admin
                where id = ?
                """, Admin.class, id);
    }

    @Override
    public int update(Admin admin) {
        return JdbcUtil.executeUpdate("update admin set name = ?, phone = ?, sex = ?, email = ?, birthday = ? where id = ?",
                admin.getName(), admin.getPhone(), admin.getSex(), admin.getEmail(), admin.getBirthday(), admin.getId());
    }

    @Override
    public int del(Integer id) {
        return JdbcUtil.executeUpdate("delete from admin where id = ?", id);
    }
}
